<?php
namespace app\home\controller;
use houdunwang\request\Request;
use system\model\User;

/**
 *  前台登录注册
 * Class Entry
 * @package app\home\controller
 */
class Login
{

    //登录
    public function login(User $user)
    {
        if(IS_POST){
            $res = (new User())->login();
            if($res['valid'])
            {
                //执行成功
                return message( $res['msg'], u('home.entry.index'), $type = 'success', $timeout = 2 );
            }else{
                //执行失败
                return message($res['msg'],'back','error');

            }
        }
        return view();
    }

    //注册方法
    public function reg(User $user)
    {
        if(IS_POST){
            $post = $_POST;
            $post['user_password'] =Crypt::encrypt($_POST['user_password']);
//            p($_POST);die;
            $res = (new User())->reg();
            if($res['valid'])
            {
                //执行添加数据
                $user->save($post);
                //执行成功
                return message( $res['msg'], u('home.login.login'), $type = 'success', $timeout = 2 );
            }else{
                //执行失败
                return message($res['msg'],'back','error');

            }
        }

        //1.加载模板
        return view();
    }

    //验证码方法
    public function code()
    {
        Code::num(1)->width(100)->height(50)->make();
    }
}